﻿import com.dmotiko.bolacua.*
import com.dmotiko.general.*
import mx.utils.Delegate;

class RoomPhotoList
extends BaseClip {
	
	private var container:MovieClip;
	/*private var photo1:RoomPhotoLoader;
	private var photo2:RoomPhotoLoader;
	private var photo3:RoomPhotoLoader;*/
	private var nCount:Number;
	private var aPhotos:Array;
	
	private function RoomPhotoList() {
		super();
		//trace("new RoomPhotoList");
		
	}
	
	public function initClip():Void {
		//trace("RoomPhotoList initClip");
		nCount = 0;
		aPhotos = new Array();
	}
		
	private function refreshData() {
		this.container = this.createEmptyMovieClip("container", 1);
		var nX:Number = 0;
		for (var i:Number = 0; i < data.length; i++) {
			var photoData:PhotoData = PhotoData(data[i]);
			var clip = this.container.attachMovie("roomPhotoLoader", "photo" + i, i);
			clip._x = nX;
			nX += 77;
			aPhotos.push( RoomPhotoLoader(clip) );
		}
		loadNextPhoto();
	}
	
	private function loadNextPhoto() {
		//trace("RoomPhotoList loadNextPhoto");
		var photo:RoomPhotoLoader = aPhotos[nCount];
		if (!photo) return;
		photo.addEventListener( MovieLoaderClip.CLIP_LOADED, Delegate.create(this, loadNextPhoto) );
		photo.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, photoSelected) );
		//trace("RoomPhotoList loadNextPhoto= "+data[nCount]);
		photo.setData( data[nCount] );
		nCount++;
	}
		
	private function photoSelected(evnt) {
		if(!evnt.loaded){
			var nIndex:Number;
			for (var i:Number = 0; i < aPhotos.length; i++) {
				if (aPhotos[i] == evnt.target) nIndex = i;				
			}
			var pSelected:RoomPhotoLoader = RoomPhotoLoader(evnt.target);
			dispatchEvent( { target: this, type: BaseClipEvent.CHANGED, index: nIndex } );
		}
	}
	
}